package com.sys.servlet.mgr.course;

import com.sys.exception.DuplicateCardException;
import com.sys.pojo.JsonResult;
import com.sys.service.CourseService;
import com.sys.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/mgr/course/add")
public class addServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cname = req.getParameter("cname");
        String ctime = req.getParameter("ctime");
        String cplace = req.getParameter("cplace");
        String tid = req.getParameter("tid");
        String stat = req.getParameter("stat");
        String produce = req.getParameter("produce");

        CourseService courseService = new CourseService();
        JsonResult jr = new JsonResult();
        try {
            courseService.add(cname,ctime,cplace,tid,stat,produce);
            jr.setCode(0);
            jr.setMsg("添加成功！");
        }catch ( DuplicateCardException e){
            jr.setCode(104);
            jr.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("操作发生错误！");
        }
        WriteJson.write(resp,jr);
    }
}
